﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.DB;
using System.Net.Mail;
using PMPUtils.Resources;
using PMPUtils.Util;
using PMPUtils.Goals;

namespace PMPUtils.Management
{
    public class ScoreboardOperator
    {
        public static void InsertScore(String goalID, String author, String type,
            String score, String reviewer, String notes)
        {
            String convertedNotes = notes.Replace("\'", "\"");

            String strInsert = "insert into scoreboard (goal_id, assignee, type, score, reviewer, notes) values " +
                " (" + goalID + ", '" + author + "', '" + type + "', " + score + ", '" + reviewer + "', '" + convertedNotes + "')";
            DBOperator localOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            localOperator.Update(strInsert);
        }

        public static void SendUpdateMessage(String goalID, String subject, String userName, String notes)
        {
            MailAddress mailFrom = ResourceOperator.GetEmailAddress(userName);

            String messageBody = notes;
            messageBody += "<br />";
            messageBody += "<br />";
            messageBody += "Check details on http://bs-pc-664:9090/goals/Dashboard.aspx";
            
            String ownerEmail = GoalsOperator.ReadGoalEmail(goalID);
            String managerEmail = ResourceOperator.ReadManagerEmailByEmail(ownerEmail);

            MailAddress owner = new MailAddress(ownerEmail);
            MailAddress manager = new MailAddress(managerEmail);
            MailAddress teamMamanger = new MailAddress("rain.wei@bleum.com");

            MailAddressCollection sendTos = new MailAddressCollection();
            sendTos.Add(owner);
            sendTos.Add(mailFrom);
            sendTos.Add(manager);
            sendTos.Add(teamMamanger);

            EmailOperator.Send(subject, messageBody, sendTos, mailFrom, true, null);
        }
    }
}
